package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.CompanyForm;
import com.vcaresolution.logistic.to.BranchTO;
import com.vcaresolution.logistic.to.CompanyTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>CompanyController</code> is used to track all login related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class CompanyController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.COMPANYFORM_MODELATTRIBUTE)
	public CompanyForm createForm(){
		return new CompanyForm();
	}
	
	private void loadRequiredDetailInModelObject(ModelAndView modelAndView){
		modelAndView.addObject(ApplicationConstants.COMPANYCITYLIST_MODELATTRIBUTE, getServiceRegistry().getCityService().findByAll());
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDCompany Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_COMPANY_URL,method = RequestMethod.GET)
	public ModelAndView loadAddCompany() {
		ModelAndView modelAndView = new ModelAndView();
		loadRequiredDetailInModelObject(modelAndView);
		modelAndView.setViewName(ApplicationConstants.ADD_COMPANY_VIEW);
		modelAndView.addObject(ApplicationConstants.COMPANYFORM_MODELATTRIBUTE, new CompanyForm());
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDCompany Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_COMPANY_URL,method = RequestMethod.POST)
	public ModelAndView processAddCompany(@Valid CompanyForm companyForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		if( !result.hasErrors() ){
			getServiceRegistry().getCompanyService().add(companyForm);
			
			redirectAttrs.addFlashAttribute(ApplicationConstants.SUCCESS_MESSAGE,getMessageSource().getMessage("companyForm.added.message", null, null));
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_COMPANY_URL, true, true, false));
		}else {
			loadRequiredDetailInModelObject(modelAndView);
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ListCompany Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_COMPANY_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<CompanyTO> companyList = getServiceRegistry().getCompanyService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.COMPANYLIST_MODELATTRIBUTE, companyList);
		modelAndView.setViewName(ApplicationConstants.LIST_COMPANY_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDCompany Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_COMPANY_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditCompany(@RequestParam(value="companyId") String companyID) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object companyObject = getServiceRegistry().getCompanyService().findById(Integer.parseInt(companyID));
		
		/**#editedByVishal*/
		List<BranchTO> associatedBranchList = getServiceRegistry().getCompanyService().getAssociatedBranchList(companyID,BranchTO.class);
		
		loadRequiredDetailInModelObject(modelAndView);
		modelAndView.addObject(ApplicationConstants.COMPANYFORM_MODELATTRIBUTE, companyObject);
		modelAndView.addObject(ApplicationConstants.BRANCHLIST_MODELATTRIBUTE, associatedBranchList);
		modelAndView.setViewName(ApplicationConstants.EDIT_COMPANY_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDCompany Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_COMPANY_URL,method = RequestMethod.POST)
	public ModelAndView processEditCompany(@Valid CompanyForm companyForm,
			final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		
		ModelAndView modelAndView = new ModelAndView();
		
		if( !result.hasErrors() ){
			
			getServiceRegistry().getCompanyService().update(companyForm);

			redirectAttrs.addFlashAttribute(ApplicationConstants.SUCCESS_MESSAGE,getMessageSource().getMessage("companyForm.updated.message", null, null));
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_COMPANY_URL, true, true, false));
		}else{
			
			String companyId = null;
			if(companyForm.getCompanyid() != null && companyForm.getCompanyid().intValue() > 0){
				companyId = String.valueOf(companyForm.getCompanyid());
			}
			/**#editedByVishal*/
			List<BranchTO> associatedBranchList = getServiceRegistry().getCompanyService().getAssociatedBranchList(companyId,BranchTO.class);
			loadRequiredDetailInModelObject(modelAndView);
			modelAndView.addObject(ApplicationConstants.BRANCHLIST_MODELATTRIBUTE, associatedBranchList);
			
		}
		
		return modelAndView;
	}
}
